home *** CD-ROM | disk | FTP | other *** search
- /*
-
- ItemOffset XFCN v1.2
-
- ©1990 Apple Computer, Inc.; by Mike Byrne
-
- ItemOffset won't initially make sense, but it has its uses. Suppose you have a comma-separated list
- of things (items to a HyperCard list), and you want to know where in the list the string "daffy"
- occurs. This will tell you what item number contains "daffy". The optional third parameter can
- be used to specify a delimiter other than a comma.
-
- Form:
- ItemOffset(<target>, <container>, [<delimiter>])
-
- # the MPW 3.2 build commands:
- C -b ItemOffset.c -mbg off
- Link -w -t STAK -c WILD -rt XFCN=608 ∂
- -m ENTRYPOINT ∂
- -sg ItemOffset ∂
- ItemOffset.c.o ∂
- "{Libraries}HyperXLib.o" ∂
- "{Libraries}Runtime.o" ∂
- "{Libraries}Interface.o" ∂
- "{CLibraries}StdCLib.o" ∂
- -o "teststack"
- */
-
- #include <Types.h>
- #include <string.h>
- #include <Packages.h>
- #include <Memory.h>
- #include <CType.h>
- #include "HyperXCmd.h"
-
- #define NULL (long) 0
- #define NIL (long) 0
-
- /* prototypes */
- void ErrorBack(XCmdPtr paramPtr, char *message);
- void MoveLockParams ( XCmdPtr paramPtr, short paramCount );
- void UnlockParams ( XCmdPtr paramPtr, short paramCount );
-
-
- pascal void EntryPoint(XCmdPtr paramPtr)
- {
- /* declarations */
- char delim;
- char target[100];
- char block[100];
- Str255 returnVal;
- short i = 0;
- short j = 0;
- short k = 0;
- short length;
- short stopLength;
- short location = 0;
- Boolean done = false;
-
-
-
- /* move high and lock the parameters. */
- MoveLockParams(paramPtr, paramPtr->paramCount);
-
- /* check for copyright or syntax help request */
- if (!strcmp( (char*)*paramPtr->params[0], "!") && (paramPtr->paramCount == 1) ) {
- ErrorBack(paramPtr, "v1.2, ©1990-1 Apple Computer, Inc.; by Mike Byrne");
- UnlockParams(paramPtr, paramPtr->paramCount);
- return;
- } else if (!strcmp ( (char*)*paramPtr->params[0], "?") && (paramPtr->paramCount == 1) ) {
- ErrorBack(paramPtr, "ItemOffset syntax is 'ItemOffset(<target>, <container>, [<delimiter>])'");
- UnlockParams(paramPtr, paramPtr->paramCount);
- return;
- }
-
- /* not a copyright or help request. */
- /* check for correct number of parameters */
- if ( (paramPtr->paramCount < 2) || (paramPtr->paramCount > 3) ) {
- ErrorBack(paramPtr, "Error: ItemOffset syntax is 'ItemOffset(<target>, <container>, [<delimiter>])'");
- UnlockParams(paramPtr, paramPtr->paramCount);
- return;
- }
-
-
- /* set up the delimiter */
- if (paramPtr->paramCount == 2) {
- delim = ',';
- } else {
- switch ( (*(paramPtr->params[2]))[0] ) {
- case 'r': case 'R':
- { delim = '\n'; break; } // a return
- case 't': case 'T':
- { delim = '\t'; break; } // a tab
- case 's': case 'S':
- { delim = ' '; break; } // a space
- default:
- { delim = (*(paramPtr->params[2]))[0]; break; }
- }
- }
-
- /* create the target string and convert to uppercase. */
- strcpy(target, (char*) *(paramPtr->params[0]) );
- length = strlen(target);
- for (i=0; i < length; i++) {
- target[i] = toupper(target[i]);
- }
-
- /* the actual searching part. */
- do {
- /* set up the searchStop */
- stopLength = strlen( (char*) *(paramPtr->params[1]) );
-
- /* check to see if we're at the end. */
- if (j>=stopLength) {
- done = true;
- location = 0;
- } else {
- /* get the block */
- for (i=0; ( ((*(paramPtr->params[1]))[j] != delim) && (i < 100) && (j < stopLength) ); i++, j++)
- { block[i] = (*(paramPtr->params[1]))[j]; }
- block[i] = '\0';
- j++;
-
- /* convert the block to uppercase */
- length = strlen(block);
- for (i=0; i < length; i++) {
- block[i] = toupper(block[i]);
- }
-
- /* check and increment */
- if (strstr(block, target)) { done = true; }
- location++;
- }
- } while (!done);
-
-
- /* convert the number to a string and go home. */
- NumToString( (long) location, &returnVal);
- p2cstr(returnVal);
- ErrorBack(paramPtr, returnVal);
- return;
-
- }
-
-
-
-
-
-
- /* allocate and load up paramPtr->returnValue with a string
- -------------------------------------------------------- */
- void ErrorBack(XCmdPtr paramPtr, char *message)
- {
- Handle mesHnd;
-
- /*
- Allocate space for an error message.
- Copy the string into it.
- Return the handle to HyperCard.
- */
- mesHnd = NewHandle((long)(strlen(message)+1));
- if (mesHnd == nil) return;
- strcpy((char *)*mesHnd,message);
- paramPtr->returnValue = mesHnd;
- }
-
-
-
- /* move high and lock down all parameters
- ----------------------------------------------------------------------- */
- void MoveLockParams ( XCmdPtr paramPtr, short paramCount )
- {
- short i;
-
- for(i=0; i <= paramCount-1; i++)
- {
- MoveHHi(paramPtr->params[i]);
- HLock(paramPtr->params[i]);
- }
- }
-
-
-
-
- /* unlock all parameter handles in the XCmdBlock
- --------------------------------------------- */
- void UnlockParams ( XCmdPtr paramPtr, short paramCount )
- { short i;
-
- for(i=0; i <= paramCount-1; i++)
- { HUnlock(paramPtr->params[i]);}
- }
-